package cn.itzd.service.impl;

import cn.itzd.config.PicCommon;
import cn.itzd.service.UploadService;
import cn.itzd.utils.FileUploadUtils;
import cn.itzd.utils.ServerUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Author: dada
 * @Version: 1.0
 * @Description:
 */
@Service
public class UploadServiceImpl implements UploadService {

    @Autowired
    private PicCommon common;

    @Autowired
    private ServerUrl serverUrl;

    @Override
    public Map<String, String> uploadFile(MultipartFile file) {
        // 上传文件路径
        PicCommon.ElPath path = common.getPath();
        // 上传并返回新文件名称
        Map<String, String> upload = FileUploadUtils.upload(path.getPath(), file);
        String urlIp = "";
        if (serverUrl.getUrl().contains("localhost")) {
            urlIp = serverUrl.getUrl() + upload.get("pathFileName");  //带有IP的图片路径路径
        }else {
            urlIp = serverUrl.getUrl() +"/v1"+ upload.get("pathFileName");  //带有IP的图片路径路径
        }
        String url = (String) upload.get("pathFileName");  //传给前端不带有IP的，会根据域名自动识别出访问哪个IP的
        upload.put("fileName",upload.get("fileName"));
        upload.put("newFileName",upload.get("newFileName"));
        upload.put("pathFileName",url);
        upload.put("pathIpFileName",urlIp);
        return upload;
    }
}
